package com.acane.instagram.mapper;

import com.acane.instagram.pojo.dto.LikeDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface LikeMapper extends BaseMapper<LikeDTO> {

    @Select("select l.* from likes l left join post p on l.post_id = p.id " +
            "where p.user_id = #{userId} order by l.timestamp desc")
    List<LikeDTO> getLikeHistory(@Param("userId") String userId);

    @Select("select * from likes where post_id = #{postId} and liker_id = #{likerId}")
    LikeDTO judgeIfAlreadyLike(@Param("postId") String postId, @Param("likerId") String likerId);

    // 将时间更新为当前时间
    @Update("UPDATE likes SET timestamp = #{timestamp} WHERE id = #{id}")
    int updateTime(@Param("id") String id, @Param("timestamp") Timestamp timestamp);

    @Select("select * from likes where post_id = #{postId} and liker_id = #{likerId}")
    LikeDTO getByPostIdAndLikerId(@Param("postId") String postId, @Param("likerId") String likerId);

    @Select("SELECT COUNT(*) FROM likes WHERE post_id = #{postId}")
    int countByPostId(String postId);
}
